package com.yifeng.repo.base.mdm.console.impl;

import com.yifeng.base.account.api.AdmOrgMasterDataRestService;
import com.yifeng.base.account.api.dto.AdmOrgBaseInfoDto;
import com.yifeng.base.account.api.dto.AdmOrgCoreInfoDto;
import com.yifeng.base.account.api.dto.AdmOrgDetailInfoDto;
import com.yifeng.base.account.api.dto.CompanyInfoDto;
import com.yifeng.base.account.api.param.AdmOrgParam;
import com.yifeng.repo.base.constant.BaseConstant;
import com.yifeng.repo.base.mdm.loader.annotation.MdmCache;
import com.yifeng.repo.base.params.ConsoleParam;
import com.yifeng.repo.base.security.proxy.CommonProxy;
import com.yifeng.repo.base.utils.codec.URLEncodeHelper;
import com.yifeng.repo.base.utils.converter.DateHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by daibing on 2024/1/30.
 */
@MdmCache(mdmTypeCode = "admOrg")
public class AdmOrgMasterDataRestServiceImpl extends CommonProxy implements AdmOrgMasterDataRestService {
    private final String mapping;

    public AdmOrgMasterDataRestServiceImpl(ConsoleParam consoleParam) {
        super(consoleParam);
        this.mapping = "/yfcloud-user/rest/admOrg";
    }

    @Override
    public List<AdmOrgCoreInfoDto> listCoreInfo(AdmOrgParam orgParam) {
        String url = consoleParam.getUrl() + mapping + "/listCoreInfo";
        Map<String, String> params = new HashMap<>();
        params.put("orgCode", orgParam.getOrgCode());
        params.put("enable", orgParam.getEnable() != null ? orgParam.getEnable().toString() : null);
        params.put("endTime", URLEncodeHelper.encode(DateHelper.getDateTime(orgParam.getEndTime())));
        params.put("offsetStartTime", URLEncodeHelper.encode(DateHelper.getDateTime(orgParam.getOffsetStartTime())));
        params.put("offsetOrgCode", orgParam.getOffsetOrgCode());
        params.put("pageSize", orgParam.getPageSize() != null ? orgParam.getPageSize().toString() : null);
        String orgRanksQueryParameters = buildCollectionQueryParameters("orgRanks", orgParam.getOrgRanks(), true);
        String queryParameters = buildQueryParameters(params) + orgRanksQueryParameters;
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgCoreInfoDto.class);
    }

    @Override
    public AdmOrgBaseInfoDto getBaseInfoByCode(String orgCode) {
        String url = consoleParam.getUrl() + mapping + "/getBaseInfoByCode";
        String queryParameters = buildSingleQueryParameters("orgCode", orgCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, AdmOrgBaseInfoDto.class);
    }

    @Override
    public List<AdmOrgBaseInfoDto> listBaseInfoByCodes(List<String> orgCodes) {
        String url = consoleParam.getUrl() + mapping + "/listBaseInfoByCodes";
        String queryParameters = buildCollectionQueryParameters("orgCodes", orgCodes);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgBaseInfoDto.class);
    }

    @Override
    public AdmOrgBaseInfoDto getBaseInfoByStoreCode(String storeCode) {
        String url = consoleParam.getUrl() + mapping + "/getBaseInfoByStoreCode";
        String queryParameters = buildSingleQueryParameters("storeCode", storeCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, AdmOrgBaseInfoDto.class);
    }

    @Override
    public List<AdmOrgBaseInfoDto> listBaseInfoByStoreCodes(List<String> storeCodes) {
        String url = consoleParam.getUrl() + mapping + "/listBaseInfoByStoreCodes";
        String queryParameters = buildCollectionQueryParameters("storeCodes", storeCodes);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgBaseInfoDto.class);
    }

    @Override
    public AdmOrgBaseInfoDto getBaseInfoByUserCode(String userCode) {
        String url = consoleParam.getUrl() + mapping + "/getBaseInfoByUserCode";
        String queryParameters = buildSingleQueryParameters("userCode", userCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, AdmOrgBaseInfoDto.class);
    }

    @Override
    public List<AdmOrgBaseInfoDto> listOwnerOrgByUserCode(String userCode) {
        String url = consoleParam.getUrl() + mapping + "/listOwnerOrgByUserCode";
        String queryParameters = buildSingleQueryParameters("userCode", userCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgBaseInfoDto.class);
    }

    @Override
    public List<AdmOrgBaseInfoDto> listPathByOrgCode(String orgCode) {
        String url = consoleParam.getUrl() + mapping + "/listPathByOrgCode";
        String queryParameters = buildSingleQueryParameters("orgCode", orgCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgBaseInfoDto.class);
    }

    @Override
    public List<AdmOrgBaseInfoDto> listPathByStoreCode(String storeCode) {
        String url = consoleParam.getUrl() + mapping + "/listPathByStoreCode";
        String queryParameters = buildSingleQueryParameters("storeCode", storeCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgBaseInfoDto.class);
    }

    @Override
    public List<AdmOrgBaseInfoDto> listPathByUserCode(String userCode) {
        String url = consoleParam.getUrl() + mapping + "/listPathByUserCode";
        String queryParameters = buildSingleQueryParameters("userCode", userCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgBaseInfoDto.class);
    }

    @Override
    public List<AdmOrgBaseInfoDto> listSubBaseInfoByOrgCode(String orgCode) {
        String url = consoleParam.getUrl() + mapping + "/listSubBaseInfoByOrgCode";
        String queryParameters = buildSingleQueryParameters("orgCode", orgCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgBaseInfoDto.class);
    }

    @Override
    public List<CompanyInfoDto> listEdifactCompany() {
        String url = consoleParam.getUrl() + mapping + "/listEdifactCompany";
        String requestUrl = String.format("%s?%s", url, BaseConstant.EMPTY);
        return this.doListCallByRpc(requestUrl, CompanyInfoDto.class);
    }

    @Override
    public List<CompanyInfoDto> listLegalCompany() {
        String url = consoleParam.getUrl() + mapping + "/listLegalCompany";
        String requestUrl = String.format("%s?%s", url, BaseConstant.EMPTY);
        return this.doListCallByRpc(requestUrl, CompanyInfoDto.class);
    }

    @Override
    public List<AdmOrgBaseInfoDto> listAllianceStoreByUserCode(String userCode) {
        String url = consoleParam.getUrl() + mapping + "/listAllianceStoreByUserCode";
        String queryParameters = buildSingleQueryParameters("userCode", userCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgBaseInfoDto.class);
    }

    @Override
    public AdmOrgDetailInfoDto getDetailInfoByCode(String orgCode) {
        String url = consoleParam.getUrl() + mapping + "/getDetailInfoByCode";
        String queryParameters = buildSingleQueryParameters("orgCode", orgCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, AdmOrgDetailInfoDto.class);
    }

    @Override
    public AdmOrgDetailInfoDto getDetailInfoByStoreCode(String storeCode) {
        String url = consoleParam.getUrl() + mapping + "/getDetailInfoByStoreCode";
        String queryParameters = buildSingleQueryParameters("storeCode", storeCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, AdmOrgDetailInfoDto.class);
    }

    @Override
    public List<AdmOrgDetailInfoDto> listDetailInfoByCodes(List<String> orgCodes) {
        String url = consoleParam.getUrl() + mapping + "/listDetailInfoByCodes";
        String queryParameters = buildCollectionQueryParameters("orgCodes", orgCodes);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AdmOrgDetailInfoDto.class);
    }

    @Override
    public String getEdifactCompanyCodeByStoreCode(String storeCode) {
        String url = consoleParam.getUrl() + mapping + "/getEdifactCompanyCodeByStoreCode";
        String queryParameters = buildSingleQueryParameters("storeCode", storeCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, String.class);
    }

}
